package nl.hr.cmi.opleiding;

import java.util.ArrayList;

import nl.hr.cmi.opleiding.utils.animation.DisplayNextView;
import nl.hr.cmi.opleiding.utils.animation.Flip3dAnimation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class Menu extends Activity {

	ImageView iv_agenda;
	ImageView iv_opleidinginformatie;
	ImageView iv_showcase;
	ImageView iv_contact;

	private boolean isFirstImage = true;

	private ArrayList<ImageView> animatedviews = new ArrayList<ImageView>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);

		iv_agenda = (ImageView) findViewById(R.id.agenda);
		iv_opleidinginformatie = (ImageView) findViewById(R.id.opleiding);
		iv_showcase = (ImageView) findViewById(R.id.showcase);
		iv_contact = (ImageView) findViewById(R.id.menu_locatie);

		iv_opleidinginformatie.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Menu.this, Opleiding.class);
				startActivity(intent);

			}
		});

		iv_agenda.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Menu.this, Agenda.class);
				startActivity(intent);

			}
		});

		iv_showcase.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Menu.this, Showcase.class);
				startActivity(intent);

			}
		});

		iv_contact.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Menu.this, Locatie.class);
				startActivity(intent);

			}
		});
		animatedviews.add(iv_showcase);
		animatedviews.add(iv_agenda);
		animatedviews.add(iv_opleidinginformatie);
		animatedviews.add(iv_contact);

		// Button b = (Button) findViewById(R.id.buttonswitch);
		// b.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// if (isFirstImage) {
		// applyRotation(0, 90);
		// isFirstImage = !isFirstImage;
		// } else {
		// applyRotation(0, -90);
		// isFirstImage = !isFirstImage;
		// }
		// }
		// });
	}

	@Override
	public void onResume() {
		super.onResume();

		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {

			public void run() {
				for (ImageView v : animatedviews) {
					if (isFirstImage) {
						applyRotation(0, 90, v);
					} else {
						applyRotation(0, -90, v);
					}
					isFirstImage = !isFirstImage;
				}
			}

		}, 1000);

	}

	private void applyRotation(float start, float end, ImageView v) {

		// Find the center of image
		final float centerX = v.getWidth() / 2.0f;
		final float centerY = v.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3dAnimation rotation = new Flip3dAnimation(start, end,
				centerX, centerY);
		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new DisplayNextView(isFirstImage, v, v));

		v.startAnimation(rotation);
	}

}
